#!/bin/sh

set_glite_ui() {
    # setup grid environment
    if [ ! -z $GANGA_GLITE_UI ]; then
        source $GANGA_GLITE_UI
    fi
}

resolve_tmpdir () {
    if [ n$AMAUSER_PLATFORM = n'grid' ]; then
        #  - fistly respect the TMPDIR setup on WN
        #  - use SCRATCH_DIRECTORY from pre-WS globus gatekeeper 
        #  - use EDG_WL_SCRATCH from gLite middleware
        #  - use /tmp dir forcely in the worst case
        if [ -z $TMPDIR ]; then
            if [ ! -z $SCRATCH_DIRECTORY ]; then
                export TMPDIR=$SCRATCH_DIRECTORY  
            elif [ ! -z $EDG_WL_SCRATCH ]; then
                export TMPDIR=$EDG_WL_SCRATCH
            else 
                export TMPDIR=`mktemp -d /tmp/ganga_scratch_XXXXXXXX`
                if [ $? -ne 0 ]; then
                    echo "cannot create and setup TMPDIR"
                    exit 1
                fi
            fi
        fi
    elif [ n$AMAUSER_PLATFORM = n'local' ]; then
        #  - check if WORKDIR is given in the LSF case
        if [ ! -z $LSB_JOBID ] && [ ! -z $WORKDIR ]; then
            export TMPDIR=`mktemp -d $WORKDIR/ganga_scratch_XXXXXXXX`
        fi
    fi
}

## function for getting STDOUT file 
get_stdout () {

    myStdOut=$(ls -l /proc/$$/fd/1 | sed 's/^.* //1')

    return $myStdOut
}

## function for getting STDERR file 
get_stderr () {

    myStdErr=$(ls -l /proc/$$/fd/2 | sed 's/^.* //1')

    return $myStdErr
}

## function for checking voms proxy information
check_voms_proxy() {

    ## redirect path to the grid proxy if there is a shared one
    ## usually the case on a local cluster
    if [ ! -z $SHR_X509_USER_PROXY ]; then
        export X509_USER_PROXY=$SHR_X509_USER_PROXY
    fi

    echo "== voms-proxy-info -all =="
    voms-proxy-info -all
    echo "===="
}

## function for printing WN env. info 
print_wn_info () {
    echo "== hostname =="
    hostname -f
    echo "===="
    echo 

    echo "== system =="
    uname -a
    echo "===="
    echo 

    echo "==  env. variables =="
    env
    echo "===="
    echo 

    echo "==  disk usage  =="
    df .
    echo "===="
    echo 
}

## function for setting up CMT environment
cmt_setup () {

    # setup ATLAS software
    unset CMTPATH
    
    export LCG_CATALOG_TYPE=lfc
    
    #  LFC Client Timeouts
    export LFC_CONNTIMEOUT=180
    export LFC_CONRETRY=2
    export LFC_CONRETRYINT=60
    
    # improve dcap reading speed
    export DCACHE_RAHEAD=TRUE
    #export DCACHE_RA_BUFFER=262144
    # Switch on private libdcap patch with improved read-ahead buffer algorithm
    export DC_LOCAL_CACHE_BUFFER=1
    if [ n$DQ2_LOCAL_SITE_ID == n'LRZ-LMU_DATADISK' ] && [ n$DATASETTYPE == n'DQ2_LOCAL' ]; then  
	export DCACHE_CLIENT_ACTIVE=1
    fi

    if [ n$AMAUSER_PLATFORM = n'grid' ]; then
        ATLAS_RELEASE_DIR=$VO_ATLAS_SW_DIR/software/$ATLAS_RELEASE
    elif [ n$AMAUSER_PLATFORM = n'local' ]; then
        ATLAS_RELEASE_DIR=$ATLAS_SOFTWARE/$ATLAS_RELEASE
    fi

    if [ ! -z `echo $ATLAS_RELEASE | grep 11.` ]; then
        source $ATLAS_RELEASE_DIR/setup.sh 
    elif [ ! -z `echo $ATLAS_RELEASE | grep 12.` ] || [ ! -z `echo $ATLAS_RELEASE | grep 13.` ] || [ ! -z `echo $ATLAS_RELEASE | grep 14.` ] || [ ! -z `echo $ATLAS_RELEASE | grep 15.` ]; then
        #if [ n$ATLAS_PROJECT = n'AtlasPoint1' ]; then
        if [ ! -z $ATLAS_PROJECT ] && [ ! -z $ATLAS_PRODUCTION ]; then
            source $ATLAS_RELEASE_DIR/cmtsite/setup.sh -tag=$ATLAS_PRODUCTION,$ATLAS_PROJECT
        elif [ ! -z $ATLAS_PROJECT ]; then
            source $ATLAS_RELEASE_DIR/cmtsite/setup.sh -tag=$ATLAS_RELEASE,$ATLAS_PROJECT
        else
            source $ATLAS_RELEASE_DIR/cmtsite/setup.sh -tag=AtlasOffline,$ATLAS_RELEASE
        fi

	# check if 64 bit was made and correct it
	if [ n$CMTCONFIG == n'x86_64-slc5-gcc43-opt'  ]; then 

	    if [ ! -z $ATLAS_PROJECT ] && [ ! -z $ATLAS_PRODUCTION ]; then
		source $ATLAS_RELEASE_DIR/cmtsite/setup.sh -tag=$ATLAS_PRODUCTION,$ATLAS_PROJECT,32,setup
	    elif [ ! -z $ATLAS_PROJECT ]; then
		source $ATLAS_RELEASE_DIR/cmtsite/setup.sh -tag=$ATLAS_RELEASE,$ATLAS_PROJECT,32,setup
	    else
		source $ATLAS_RELEASE_DIR/cmtsite/setup.sh -tag=AtlasOffline,$ATLAS_RELEASE,32,setup
	    fi
	fi    
    fi

    # print relevant env. variables for debug 
    echo "CMT setup:"
    env | grep 'CMT'
    echo "SITE setup:"
    env | grep 'SITE'
}

## this is only for Stoomboot/NIKHEF desktop
fix_gcc_issue_sl5_local () {
    source /data/atlas/offline/compiler/x86_64-slc5-4.3.2/setup.sh
}

## this is only for Grid jobs
fix_gcc_issue_sl5 () {

    # fix SL5 gcc/g++ problem - need to use version 3.4
    RHREL=`cat /etc/redhat-release`
    SC51=`echo $RHREL | grep -c 'Scientific Linux CERN SLC release 5'`
    SC52=`echo $RHREL | grep -c 'Scientific Linux SL release 5'`

    if [ $SC51 -gt 0 ] || [ $SC52 -gt 0 ]; then 
        gcc34_path=`which gcc34`

        if [ $? -eq 0 ]; then
            if [ ! -d comp ]; then
                mkdir comp
            fi
            ln -sf $gcc34_path comp/gcc
        fi

        gpp34_path=`which g++34`
        if [ $? -eq 0 ]; then
            if [ ! -d comp ]; then
                mkdir comp
            fi
            ln -sf $gpp34_path comp/g++
        fi

        export PATH=$PWD/comp:$PATH
    fi
}

## function for setting up athena runtime environment, no compilation
runtime_setup () {
    if [ ! -z `echo $ATLAS_RELEASE | grep 11.` ]
    then
        source $SITEROOT/dist/$ATLAS_RELEASE/Control/AthenaRunTime/AthenaRunTime-*/cmt/setup.sh
    elif [ ! -z `echo $ATLAS_RELEASE | grep 12.` ] || [ ! -z `echo $ATLAS_RELEASE | grep 13.` ] || [ ! -z `echo $ATLAS_RELEASE | grep 14.` ] || [ ! -z `echo $ATLAS_RELEASE | grep 15.` ]
    then
        source $SITEROOT/AtlasOffline/$ATLAS_RELEASE/AtlasOfflineRunTime/cmt/setup.sh
        if [ ! -z $ATLAS_PRODUCTION_ARCHIVE ]
        then
            wget $ATLAS_PRODUCTION_ARCHIVE
            export ATLAS_PRODUCTION_FILE=`ls AtlasProduction*.tar.gz`
            tar xzf $ATLAS_PRODUCTION_FILE
            export CMTPATH=`ls -d $PWD/work/AtlasProduction/*`
            export MYTEMPDIR=$PWD
            cd AtlasProduction/*/AtlasProductionRunTime/cmt
            cmt config
            source setup.sh
            echo $CMTPATH
            cd $MYTEMPDIR
        fi
    fi
}

## function for setting up athena runtime environment 
ama_setup () {

    echo "Setting up the AMAUser environment ..."

    retcode=0

    if [ -e amauser.tgz ]; then
        tar xvzf amauser.tgz
        source $SITEROOT/AtlasOffline/$ATLAS_RELEASE/AtlasOfflineRunTime/cmt/setup.sh
        export PATH=InstallArea/i686-slc5-gcc43-opt/bin:$PATH
        export LD_LIBRARY_PATH=InstallArea/i686-slc5-gcc43-opt/lib:$LD_LIBRARY_PATH

        ## a trick to force using a specific wrapper for FileStager
        if [ -e lcg-cp ]; then
            chmod +x lcg-cp
            alias lcg-cp.org=`which lcg-cp`
            export PATH=$PWD:$PATH
        fi
    else
        retcode=1
    fi

    return $retcode
}

ama_run () {

    sampleFile=$1
    amaConfig=$2
    maxEvt=$3
    amaFlags=${@:4}

    echo "running AMA in Standalone mode ..."
    
    AMADriver.exe $sampleFile $maxEvt ConfigFile=$amaConfig $amaFlags

    return $?
}

